/*Vladimir Sarkisyan 312636962*/
package GUI;

import java.io.FileNotFoundException;
import java.util.ArrayList;

import javax.swing.JLabel;

import main.XMLReader;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GUIview  {
	private Text textCurrentPath;
	private Text textRelativeExpression;
	private Button OpenXMLFile;
	private Text RelativeExpression;
	private Text CurrentPath;
	Display display;
	Shell shell;
	private List ElemantsList;
	private Button left;
	private Button right;
	private Label labelText;
	private Text text;
	private Label attributes;
	private Label elaments;
	private Button back;
	private List AttributesList;
	private CLabel comments;

	private Button nodeListButtons[] = new Button[7];
	private Button close;
	private Image viewImage;
	private Image closeImage;
	private Image pic;
	private ArrayList<String> elemantsArrList;
	private String currentPath;
	private String relativeExpression;
	private ArrayList<Node> nodeList;
	private int buttonCounter;
	//the GUI is a static
	public static GUIview instance = new GUIview();
	
	
	private GUIview(){
		elemantsArrList = new ArrayList<String>();
		nodeList = new ArrayList<Node>();
		buttonCounter = 0;
	}
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	 * @throws Exception 
	*/
	public void showGUI() throws Exception {
		display = Display.getDefault();
		shell = new Shell(display, 0);

		// initialize the GUI
		initGUI();
		// initialize the Listeners of the buttons
		initializeListeners();
		
		//for debuging

		//String path = new String("E:\\programs\\eclipse\\Workspace\\DBProject\\books2.xml") ;
		//System.out.println(path);
		//XMLReader.instance.startRead(path);
		//XMLReader.instance.openCurrentPath("//inventory//book//chapter");
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}



	private void initGUI() {
		try {
			
			//array list of the elemant
			elemantsArrList = new ArrayList<String>();
			viewImage = new Image(Display.getDefault(),"blueSky.jpg");
			//set the view image of the program
			shell.setBackgroundImage(viewImage);
			FormLayout thisLayout = new FormLayout();
			shell.setLayout(thisLayout);
			shell.setSize(850, 600);
			
			{
				FormData leftLData = new FormData();
				leftLData.left =  new FormAttachment(0, 1000, 90);
				leftLData.top =  new FormAttachment(0, 1000, 194);
				leftLData.width = 42;
				leftLData.height = 42;
				left = new Button(shell, SWT.PUSH | SWT.CENTER);
				left.setLayoutData(leftLData);
				left.setEnabled(false);
				
				left.setImage(new Image(display, "left2.jpg"));
			}
			{
				right = new Button(shell, SWT.PUSH | SWT.CENTER);
				FormData rightLData = new FormData();
				rightLData.left =  new FormAttachment(0, 1000, 742);
				rightLData.top =  new FormAttachment(0, 1000, 194);
				rightLData.width = 42;
				rightLData.height = 42;
				right.setLayoutData(rightLData);
				right.setEnabled(false);
								
				right.setImage(new Image(display, "right2.jpg"));
				//right.setText("right");
			}

			{
				nodeListButtons[0] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons0FileLData = new FormData();
				nodeListButtons0FileLData.left =  new FormAttachment(0, 1000, 158);
				nodeListButtons0FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons0FileLData.width = 80;
				nodeListButtons0FileLData.height = 70;
		
				nodeListButtons[0].setFont(new Font(display, "David", 8, SWT.NORMAL));
				nodeListButtons[0].setForeground(new Color(display, 250, 250, 250));
				nodeListButtons[0].setEnabled(false);
				//FormData nodeListButtons0FileLData = new FormData();
				nodeListButtons[0].setLayoutData(nodeListButtons0FileLData);
			}
			
			{
				nodeListButtons[1] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons1FileLData = new FormData();
				nodeListButtons1FileLData.left =  new FormAttachment(0, 1000, 239);
				nodeListButtons1FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons1FileLData.width = 80;
				nodeListButtons1FileLData.height = 70;
				nodeListButtons[1].setLayoutData(nodeListButtons1FileLData);
				nodeListButtons[1].setEnabled(false);
				//nodeListButtons[1].setImage(black);
			}		
			
			{
				nodeListButtons[2] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons2FileLData = new FormData();
				nodeListButtons2FileLData.left =  new FormAttachment(0, 1000, 318);
				nodeListButtons2FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons2FileLData.width = 80;
				nodeListButtons2FileLData.height = 70;
				nodeListButtons[2].setLayoutData(nodeListButtons2FileLData);
				nodeListButtons[2].setEnabled(false);
				//nodeListButtons[2].setImage(black);
			}	
			
			{
				nodeListButtons[3] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons3FileLData = new FormData();
				nodeListButtons3FileLData.left =  new FormAttachment(0, 1000, 398);
				nodeListButtons3FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons3FileLData.width = 80;
				nodeListButtons3FileLData.height = 70;
				nodeListButtons[3].setLayoutData(nodeListButtons3FileLData);
				nodeListButtons[3].setEnabled(false);
				//nodeListButtons[3].setImage(black);
			}	
			
			{
				nodeListButtons[4] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons4FileLData = new FormData();
				nodeListButtons4FileLData.left =  new FormAttachment(0, 1000, 478);
				nodeListButtons4FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons4FileLData.width = 80;
				nodeListButtons4FileLData.height = 70;
				nodeListButtons[4].setLayoutData(nodeListButtons4FileLData);
				nodeListButtons[4].setEnabled(false);
				//nodeListButtons[4].setImage(black);
			}				
			
			{
				nodeListButtons[5] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons5FileLData = new FormData();
				nodeListButtons5FileLData.left =  new FormAttachment(0, 1000, 558);
				nodeListButtons5FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons5FileLData.width = 80;
				nodeListButtons5FileLData.height = 70;
				nodeListButtons[5].setLayoutData(nodeListButtons5FileLData);
				nodeListButtons[5].setEnabled(false);
				//nodeListButtons[5].setImage(black);
			}	
			
			{
				nodeListButtons[6] = new Button(shell, SWT.PUSH | SWT.CENTER | SWT.BORDER);
				FormData nodeListButtons6FileLData = new FormData();
				nodeListButtons6FileLData.left =  new FormAttachment(0, 1000, 638);
				nodeListButtons6FileLData.top =  new FormAttachment(0, 1000, 180);
				nodeListButtons6FileLData.width = 80;
				nodeListButtons6FileLData.height = 70;	
				nodeListButtons[6].setLayoutData(nodeListButtons6FileLData);
				nodeListButtons[6].setEnabled(false);
				//nodeListButtons[6].setImage(black);
			}
			{
				labelText = new Label(shell, SWT.NONE);
				FormData labelTextLData = new FormData();
				labelTextLData.left =  new FormAttachment(0, 1000, 140);
				labelTextLData.top =  new FormAttachment(0, 1000, 299);
				labelTextLData.width = 80;
				labelTextLData.height = 20;
				labelText.setLayoutData(labelTextLData);				
				labelText.setText("Name/Value:");
				
				labelText.setFont(new Font(display, "Courier New", 10, SWT.NORMAL));
				labelText.setBackgroundImage(viewImage);
			}
			{
				text = new Text(shell, SWT.NONE);
				FormData textLData = new FormData();
				textLData.left =  new FormAttachment(0, 1000, 232);
				textLData.top =  new FormAttachment(0, 1000, 296);
				textLData.width = 487;
				textLData.height = 23;
				text.setLayoutData(textLData);
				text.setText("");
				
				text.setFont(new Font(display, "David", 14, SWT.NORMAL));
			}
			
			{
				attributes = new Label(shell, SWT.NONE);
				FormData attributesLData = new FormData();
				attributesLData.left =  new FormAttachment(0, 1000, 420);
				attributesLData.top =  new FormAttachment(0, 1000, 369);
				attributesLData.width = 89;
				attributesLData.height = 19;
				attributes.setLayoutData(attributesLData);
				attributes.setText("Attributes:");
				
				attributes.setFont(new Font(display, "Courier New", 10, SWT.BOLD));
				attributes.setBackgroundImage(viewImage);
			}
			{
				elaments = new Label(shell, SWT.NONE);
				FormData elamentsLData = new FormData();
				elamentsLData.left =  new FormAttachment(0, 1000, 90);
				elamentsLData.top =  new FormAttachment(0, 1000, 369);
				elamentsLData.width = 89;
				elamentsLData.height = 19;
				elaments.setLayoutData(elamentsLData);
				elaments.setText("Elaments:");
				
				elaments.setFont(new Font(display, "Courier New", 10, SWT.BOLD));
				elaments.setBackgroundImage(viewImage);
			}
			
			{
				FormData backLData = new FormData();
				backLData.left =  new FormAttachment(0, 1000, 742);
				backLData.top =  new FormAttachment(0, 1000, 443);
				backLData.width = 48;
				backLData.height = 48;
				back = new Button(shell, SWT.PUSH | SWT.CENTER);
				back.setLayoutData(backLData);
				back.setEnabled(false);
				
				Image backImage = new Image(Display.getDefault(),"Back.jpg");
				
				back.setImage(backImage);
			}
			{
				FormData AttributesListLData = new FormData();
				AttributesListLData.left =  new FormAttachment(0, 1000, 395);
				AttributesListLData.top =  new FormAttachment(0, 1000, 394);
				AttributesListLData.width = 259;
				AttributesListLData.height = 160;
				AttributesList = new List(shell, SWT.BORDER);
				AttributesList.setLayoutData(AttributesListLData);
			}
			{
				FormData ElemantsListLData = new FormData();
				ElemantsListLData.left =  new FormAttachment(0, 1000, 65);
				ElemantsListLData.top =  new FormAttachment(0, 1000, 394);
				ElemantsListLData.width = 259;
				ElemantsListLData.height = 160;
				ElemantsList = new List(shell, SWT.BORDER | SWT.SINGLE| SWT.V_SCROLL);
				ElemantsList.setLayoutData(ElemantsListLData);
			}
			
			{
				FormData commentsLData = new FormData();
				commentsLData.left =  new FormAttachment(0, 1000, 298);
				commentsLData.top =  new FormAttachment(0, 1000, 118);
				commentsLData.width = 432;
				commentsLData.height = 21;
				comments = new CLabel(shell, SWT.NONE);
				comments.setLayoutData(commentsLData);
				comments.setForeground(new Color(display, 150, 0, 0));
				comments.setBackground(viewImage);
				//comments.setBackground(new Color(display,0,250,250));
			}
			
			{
				FormData closeLData = new FormData();
				closeLData.left =  new FormAttachment(0, 1000, 776);
				closeLData.top =  new FormAttachment(0, 1000, 30);
				closeLData.width = 32;
				closeLData.height = 32;
				close = new Button(shell, SWT.PUSH | SWT.CENTER);			
				close.setLayoutData(closeLData);
				
				//initialize the sign of the close button
				closeImage = new Image(Display.getDefault(),"close.png");
				close.setImage(closeImage);
				
			}			
			
			{
				OpenXMLFile = new Button(shell, SWT.PUSH | SWT.CENTER);
				FormData OpenXMLFileLData = new FormData();
				OpenXMLFileLData.left =  new FormAttachment(0, 1000, 65);
				OpenXMLFileLData.top =  new FormAttachment(0, 1000, 62);
				OpenXMLFileLData.width = 120;
				OpenXMLFileLData.height = 51;
				OpenXMLFile.setLayoutData(OpenXMLFileLData);
				//OpenXMLFile.setText("Open XML file");
				
				pic = new Image(Display.getDefault(),"open.jpg");
				OpenXMLFile.setImage(pic);
				
			}
			{
				RelativeExpression = new Text(shell, SWT.READ_ONLY);
				FormData RelativeExpressionLData = new FormData();
				RelativeExpressionLData.left =  new FormAttachment(0, 1000, 298);
				RelativeExpressionLData.top =  new FormAttachment(0, 1000, 79);
				RelativeExpressionLData.width = 98;
				RelativeExpressionLData.height = 19;
				RelativeExpression.setLayoutData(RelativeExpressionLData);
				RelativeExpression.setText("Relative Expression:");
				//RelativeExpression.setForeground(new Color(display,250,250,250));
				
				RelativeExpression.setBackgroundImage(viewImage);
				
				RelativeExpression.setBackground(new Color(display,0,80,200));
			}
			{
				CurrentPath = new Text(shell, SWT.READ_ONLY);
				FormData CurrentPathLData = new FormData();
				CurrentPathLData.left =  new FormAttachment(0, 1000, 327);
				CurrentPathLData.top =  new FormAttachment(0, 1000, 30);
				CurrentPathLData.width = 69;
				CurrentPathLData.height = 20;
				CurrentPath.setLayoutData(CurrentPathLData);
				CurrentPath.setText("Current Path:");
				//CurrentPath.setForeground(new Color(display,250,250,250));
				
				Image butonImage = new Image(Display.getDefault(),"skyL.jpg");
				
				CurrentPath.setBackgroundImage(viewImage);
				//color = new Image(Display.getDefault(),"red.jpg");
				//CurrentPath.setBackground(new Color(display,0,250,250));
			}
			{
				textRelativeExpression = new Text(shell, SWT.BORDER);
				FormData textRelativeExpressionLData = new FormData();
				textRelativeExpressionLData.left =  new FormAttachment(0, 1000, 414);
				textRelativeExpressionLData.top =  new FormAttachment(0, 1000, 75);
				textRelativeExpressionLData.width = 304;
				textRelativeExpressionLData.height = 17;
				textRelativeExpression.setLayoutData(textRelativeExpressionLData);
			}
			{
				textCurrentPath = new Text(shell, SWT.BORDER);
				FormData textCurrentPathLData = new FormData();
				textCurrentPathLData.left =  new FormAttachment(0, 1000, 414);
				textCurrentPathLData.top =  new FormAttachment(0, 1000, 25);
				textCurrentPathLData.width = 304;
				textCurrentPathLData.height = 19;
				textCurrentPath.setLayoutData(textCurrentPathLData);
				
			}
			shell.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/*
	 * the function initialize the Listeners of the buttons
	 */
	private void initializeListeners(){
		
		Listener closeListener = new Listener() {

			@Override
			public void handleEvent(Event event) {
				// if the close button was pushed close shell				
				if(event.widget == close){
					shell.dispose();
				}				
			}
			
		};
		//end of the closeListener
		
		//add the listener to the close button
		close.addListener(SWT.Selection, closeListener);
		
		
		
		SelectionAdapter selectionAdapter = new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent selectionEvent){
				
				try{
				//open the XML file	
				openXMLFile(selectionEvent);
				
				}
				
                catch ( SAXParseException exception ) {
                	//System.out.println("fatal");
                	comments.setText("The file can not be opened");
                 }

                 catch ( SAXException  exception) {
                     //System.out.println("in error");
                     exception.printStackTrace();         
                 }

                 catch ( FileNotFoundException fileNotFound) {
                     comments.setText("DTD not found");
                 }				
				
				catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
		};
		//add the listener
		OpenXMLFile.addSelectionListener(selectionAdapter);
		
		
		MouseTrackListener mouseTrackListener = new MouseTrackListener() {
			
			@Override
			public void mouseHover(MouseEvent arg0) {
				// TODO Auto-generated method stub
				int indx =ElemantsList.getItemHeight();
				
				int indx2 = arg0.y;
				int indx3 = arg0.y/ElemantsList.getItemHeight();		
				//System.out.println("indx: " + indx);
				//System.out.println("indx2: " + indx2);
				//System.out.println("indx3: " + indx3);
				
				//System.out.println("ElemantsList.getItemCount() : " + ElemantsList.getItemCount());
				if(ElemantsList.getItemCount()>indx3){
					String item = ElemantsList.getItem(indx3);
					//System.out.println("item: "+item);
					//ElemantsList.setForeground(indx3, new Color(display, 100, 100, 100));
					
					ElemantsList.setToolTipText(item);
					
					}
			}
			
			@Override
			public void mouseExit(MouseEvent arg0) {
				// TODO Auto-generated method stub		
			}
			
			@Override
			public void mouseEnter(MouseEvent arg0) {
				// TODO Auto-generated method stub
				//int indx =ElemantsList.getFocusIndex();
				//System.out.println("indx: " + indx);
				//ElemantsList.setForeground(new Color(display, 100, 100, 100));						
				
			}
		};
		
		//add to the ElemantsList
		ElemantsList.addMouseTrackListener(mouseTrackListener);
		
		//for the attrubutes
		MouseTrackListener mouseTrackListener2 = new MouseTrackListener() {
			
			@Override
			public void mouseHover(MouseEvent arg0) {
				// TODO Auto-generated method stub
				int indx =AttributesList.getItemHeight();
				
				int indx2 = arg0.y;
				int indx3 = arg0.y/AttributesList.getItemHeight();

				if(AttributesList.getItemCount()>indx3){
					String item = AttributesList.getItem(indx3);		
					AttributesList.setToolTipText(item);
					
					}
			}
			
			@Override
			public void mouseExit(MouseEvent arg0) {
				// TODO Auto-generated method stub		
			}
			
			@Override
			public void mouseEnter(MouseEvent arg0) {					
			}
		};		
		//add the listener
		AttributesList.addMouseTrackListener(mouseTrackListener2);
		
		MouseListener mouseListenernew  = new MouseListener() {

		      public void mouseDoubleClick(MouseEvent arg0) {
		        //System.out.println("mouseDoubleClick");
		    	  int indx3 = arg0.y/ElemantsList.getItemHeight();
		    	  if(ElemantsList.getItemCount()>indx3){
						String item = ElemantsList.getItem(indx3);
						//System.out.println("check");
						//System.out.println("item: "+item);
						//ElemantsList.setForeground(indx3, new Color(display, 100, 100, 100));
						
						addRelativeExpression("/" + item);
						//get the childrens of the Node
						XMLReader.instance.findTheNode(item);
						//setNameValueText(item);
						
					}
		    	  
		      }

		      //when pressed
		      public void mouseDown(MouseEvent arg0) {
		        //System.out.println("mouseDown");

		      }

		      public void mouseUp(MouseEvent arg0) {
		        //System.out.println("mouseUp");

		      }
		    };
		//add to the ElemantsList
		ElemantsList.addMouseListener(mouseListenernew);
		
		//Listener to button back
		Listener backListener = new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				// TODO Auto-generated method stub
				XMLReader.instance.back();
			}
		};
		
		//add the listener to the button back
		back.addListener(SWT.Selection, backListener);
		
		//listener for the current path
		KeyListener keyListener = new KeyListener() {
			String line;
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				//if pushed enter
				if(arg0.keyCode == 13){
					//check if there is file open
					if(XMLReader.instance.isFileOpen()){
						//take the user input 
						line = textCurrentPath.getText();
						//text.setText("");
						//put this input in the big border and send to the interpreter
						XMLReader.instance.openCurrentPath(line);
						//interpreter.start(line);
					}
					else{
						comments.setText("There have not opened file");
						textCurrentPath.setText("");
					}
				}		
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		};
		//add the listener to the text
		textCurrentPath.addKeyListener(keyListener);
		
		Listener sidesButtonListener = new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				int num;
				System.out.println("in the Sides button event");
				//for the right arrow
				if(arg0.widget == right){
					//enable left
					left.setEnabled(true);
					buttonCounter++;
					//check if have to disable					
					if((buttonCounter + 7) >= nodeList.size()){
						right.setEnabled(false);
					}
					//update the buttons
					for(int i=1; i<=nodeList.size(); i++){
						num = i + buttonCounter;
						updateButton(nodeList.get(i-1).getNodeName() + "["+num+"]", i-1, true);
					}
					
				}
				//for the left arrow
				if(arg0.widget == left){
					buttonCounter--;
					//enable right
					right.setEnabled(true);
					if(buttonCounter == 0){
						left.setEnabled(false);
					}
					//update the buttons
					for(int i=1; i<=nodeList.size(); i++){
						num = i + buttonCounter;
						updateButton(nodeList.get(i-1).getNodeName() + "["+num+"]", i-1, true);
					}
				}
			}
		}; 
		//add the listeners
		right.addListener(SWT.Selection, sidesButtonListener);
		left.addListener(SWT.Selection, sidesButtonListener);
		
		
		
		Listener butonListener = new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				int num;
				//System.out.println("in the button event");
				//first button
				if(arg0.widget == nodeListButtons[0]){
					openButtonInformation(0);
				}
				//second button
				if(arg0.widget == nodeListButtons[1]){
					openButtonInformation(1);
				}
				//third button
				if(arg0.widget == nodeListButtons[2]){
					openButtonInformation(2);
				}
				//fourth button
				if(arg0.widget == nodeListButtons[3]){
					openButtonInformation(3);
				}
				//fived button
				if(arg0.widget == nodeListButtons[4]){
					openButtonInformation(4);
				}
				//sixth button
				if(arg0.widget == nodeListButtons[5]){
					openButtonInformation(5);
				}
				//seventh button
				if(arg0.widget == nodeListButtons[6]){
					openButtonInformation(6);
				}
			}
			
		}; 
		//add the listeners
		for(int i=0; i<7; i++){
			nodeListButtons[i].addListener(SWT.Selection, butonListener);
		}
	}
	//**************************end of the function initializeListeners()*********************************
	//*******************************************************************************************************8
	
	private void openXMLFile(SelectionEvent selectionEvent) throws Exception{
		
		FileDialog fileDialog=new FileDialog(shell, SWT.OPEN);
		fileDialog.setFilterExtensions(new String[]{"*.xml"});
								
		String path = fileDialog.open();
		//for debuging			
		System.out.println(path);
		
		//initialize 
		comments.setText("");
		textCurrentPath.setText("");
		textRelativeExpression.setText("");

		int len;
		len = nodeList.size();
		for(int i=0; i<len; i++){
			nodeList.remove(0);
		}
		len = elemantsArrList.size();
		for(int i=0; i<len; i++){
			elemantsArrList.remove(0);
		}		
		//initialize the buttons
		for(int i=0; i<7; i++){
			nodeListButtons[i].setText("");
			nodeListButtons[i].setEnabled(false);
		}
		right.setEnabled(false);
		left.setEnabled(false);
		back.setEnabled(false);
		
		if (path != null){
			XMLReader.instance.startRead(path);
		}
	}
	
	
	private void openButtonInformation(int numBut){
		//get the propiate node if the list not empty
		if(!nodeList.isEmpty()){
			Node node = nodeList.get(numBut+buttonCounter);
			XMLReader.readChildrens(node);
		}
	}	
	//end of openButtonInformation
	
	public void updateComments(String coment){
		
		comments.setText(coment);
		
	}
	
	
	/*
	 * the function is updating the buttons 
	 */
	public void updateButtons(ArrayList<Node> nodeLst){
		
		//update the class variable
		nodeList = nodeLst;
		buttonCounter = 0;
		int num;
		//check if to enabled the buttons
		if(nodeLst.size()>7){
			right.setEnabled(true);
		}
		else{
			right.setEnabled(false);
			left.setEnabled(false);
		}
		
		//update the buttons
		for(int i=0; (i<nodeLst.size())&&(i<7); i++){
			num = i+1;
			updateButton(nodeLst.get(i).getNodeName()+" ["+num+"]", i, true);
		}
	}
	
	/*
	 * the function is updating button
	 */
	public void updateButton(String nodeName, int i, boolean enabled){
		//check the buttons
		if(i<7){
			nodeListButtons[i].setText(nodeName);
			nodeListButtons[i].setEnabled(enabled);
		}
	}

	/*
	 * the function is write the path the CurrentPath 
	 */	
	public void updateCurrentPath(String path){
		textCurrentPath.setText(path);
		//System.out.println("CurrentnewPath: " + path);
		currentPath = path;		
	}
	//the function is add text to CurrentPath 
	public void addCurrentPath(String path){
		String temp = textCurrentPath.getText();
		String newPath = new String(temp + path );
		//System.out.println("CurrentnewPath: " + newPath);
		textCurrentPath.setText(newPath);
		currentPath = newPath;
	}
	public void backCurrentPath(String path){
		
	}	
	
	//return the string currentPath
	public String getStringCurrentPath(){
		return currentPath;
	}
	
	/*
	 * the function is write the path the RelativeExpression 
	 */		
	public void updateRelativeExpression(String path){
		textRelativeExpression.setText(path);
		//System.out.println("CurrentnewPath: " + path);
		relativeExpression = path;
	}
	//the function is add text to RelativeExpression
	public void addRelativeExpression(String path){
		String temp = textRelativeExpression.getText();
		String newPath = new String(temp + path);
		//System.out.println("RelativeEnewPath: " + newPath);
		textRelativeExpression.setText(newPath);
		relativeExpression = newPath;
	}	
	public void backRelativeExpression(){
		//System.out.println("in backRelativeExpression");	
		String oldPath = textRelativeExpression.getText();
		//split
		String reg = "/";
		String [] arrStr = oldPath.split(reg);
		/*for(int i=0; i<arrStr.length; i++){
			System.out.println("arrStr["+ i+ "]: " + arrStr[i]);
		}*/
		
		String newPath = "";
		for(int i=1; i<(arrStr.length-1); i++){
			newPath = newPath + "/" + arrStr[i];
		}
		//System.out.println("newPath: " + newPath);
		textRelativeExpression.setText(newPath);
		
	}		
	
	
	//the function is add line to ElemtList
	public void addToElemtList(String str){
		ElemantsList.add(str);
	}
	//the function is delete the data in ElamentsList
	public void deleteElamentsList(){
			//System.out.println("in deleteElamentsList " );
			ElemantsList.removeAll();
			//System.out.println("ElemantsList.getItemCount() : " + ElemantsList.getItemCount());
	}	
	
	//the function is add line to AttributesList
	public void addToAttributesList(String str){
		AttributesList.add(str);
	}
	//the function is delete the data in ElamentsList
	public void deleteAttributesList(){
		AttributesList.removeAll();
	}
	
	
	/*
	//the function is add elements to ElemantsArrList
	public void addToElemantsArrList(String element){
		elemantsArrList.add(element);
	}
	//the function is remove all the elements from the arrayList
	public void emptyElemantsArrList(){
		elemantsArrList.clear();
	}	*/
	
	//enable the button back
	public void enableButtonBack(){
		back.setEnabled(true);
	}
	//disable the button back
	public void disableButtonBack(){
		back.setEnabled(false);
	}

	public void setNameValueText(String name){
		text.setText(name);
	}
 }
